/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmCanchas.java
 *
 * Created on 14/08/2009, 02:08:07 PM
 */

package pe.edu.cibertec.reservas.view.desk;

import java.util.ArrayList;
import java.util.Date;
import javax.swing.table.AbstractTableModel;
import pe.edu.cibertec.reservas.bean.Cancha;
import pe.edu.cibertec.reservas.control.GestorCancha;

/**
 *
 * @author avillegas
 */
public class frmCancha extends javax.swing.JInternalFrame {
    private int intOpcion;
    private static int NUEVO = 1;
    private static int MODIFICAR = 2;


    /** Creates new form frmCanchas */
    public frmCancha() {
        super("Mantenimiento de Canchas",false,true,false,true);
        initComponents();
        refrescarGrid();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtDescripcion = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        txtCapacidad = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        txtFechaUltMnat = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblCanchas = new javax.swing.JTable();
        jMenuBar1 = new javax.swing.JMenuBar();
        mnNuevo = new javax.swing.JMenu();
        mnGrabar = new javax.swing.JMenu();
        mnEditar = new javax.swing.JMenu();
        mnEliminar = new javax.swing.JMenu();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Cancha"));

        jLabel1.setText("Código:");

        txtCodigo.setEnabled(false);

        jLabel5.setText("Descripción:");

        jLabel10.setText("Capacidad::");

        jLabel11.setText("Fecha Mantenimiento:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(42, 42, 42)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel10, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel11, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtFechaUltMnat, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCapacidad, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(66, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(txtCapacidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(txtFechaUltMnat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(48, 48, 48))
        );

        tblCanchas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblCanchas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblCanchasMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblCanchas);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 368, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(32, Short.MAX_VALUE))
        );

        mnNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pe/edu/cibertec/reservas/view/desk/images/group_blue_new.png"))); // NOI18N
        mnNuevo.setToolTipText("Nuevo");
        mnNuevo.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/pe/edu/cibertec/reservas/view/desk/images/group_grey_new.png"))); // NOI18N
        mnNuevo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnNuevoMouseClicked(evt);
            }
        });
        mnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnNuevoActionPerformed(evt);
            }
        });
        jMenuBar1.add(mnNuevo);

        mnGrabar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pe/edu/cibertec/reservas/view/desk/images/group_blue_add.png"))); // NOI18N
        mnGrabar.setToolTipText("Grabar");
        mnGrabar.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/pe/edu/cibertec/reservas/view/desk/images/group_grey_add.png"))); // NOI18N
        mnGrabar.setEnabled(false);
        mnGrabar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnGrabarMouseClicked(evt);
            }
        });
        mnGrabar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnGrabarActionPerformed(evt);
            }
        });
        jMenuBar1.add(mnGrabar);

        mnEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pe/edu/cibertec/reservas/view/desk/images/group_blue_edit.png"))); // NOI18N
        mnEditar.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/pe/edu/cibertec/reservas/view/desk/images/group_grey_edit.png"))); // NOI18N
        mnEditar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnEditarMouseClicked(evt);
            }
        });
        mnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnEditarActionPerformed(evt);
            }
        });
        jMenuBar1.add(mnEditar);

        mnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pe/edu/cibertec/reservas/view/desk/images/group_blue_remove.png"))); // NOI18N
        mnEliminar.setToolTipText("Eliminar");
        mnEliminar.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/pe/edu/cibertec/reservas/view/desk/images/group_grey_remove.png"))); // NOI18N
        mnEliminar.setEnabled(false);
        mnEliminar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnEliminarMouseClicked(evt);
            }
        });
        mnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnEliminarActionPerformed(evt);
            }
        });
        jMenuBar1.add(mnEliminar);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void mnNuevoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnNuevoMouseClicked
        intOpcion = NUEVO;
        limpiarText();
        mnNuevo.setEnabled(false);
        mnGrabar.setEnabled(true);
        mnEditar.setEnabled(false);
}//GEN-LAST:event_mnNuevoMouseClicked

    private void mnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnNuevoActionPerformed
        intOpcion = NUEVO;
        limpiarText();
}//GEN-LAST:event_mnNuevoActionPerformed

    private void mnGrabarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnGrabarMouseClicked

        if (intOpcion == NUEVO) {

                GestorCancha.add(new Cancha(0,
                        Integer.parseInt(txtCapacidad.getText()),
                        java.sql.Date.valueOf(txtFechaUltMnat.getText()),
                        txtDescripcion.getText()));


            
        } else {

                GestorCancha.update(new Cancha(Integer.parseInt(txtCodigo.getText()),
                        Integer.parseInt(txtCapacidad.getText()),
                        java.sql.Date.valueOf(txtFechaUltMnat.getText()),
                        txtDescripcion.getText()));

            }

        
        mnGrabar.setEnabled(false);
        mnNuevo.setEnabled(true);
        mnEditar.setEnabled(true);
        refrescarGrid();
        limpiarText();
    }//GEN-LAST:event_mnGrabarMouseClicked

    private void mnGrabarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnGrabarActionPerformed

        if (intOpcion == NUEVO) {

                GestorCancha.add(new Cancha(0,
                        Integer.parseInt(txtCapacidad.getText()),
                        java.sql.Date.valueOf(txtFechaUltMnat.getText()),
                        txtDescripcion.getText()));



        } else {

                GestorCancha.update(new Cancha(Integer.parseInt(txtCodigo.getText()),
                        Integer.parseInt(txtCapacidad.getText()),
                        java.sql.Date.valueOf(txtFechaUltMnat.getText()),
                        txtDescripcion.getText()));

            }


        mnGrabar.setEnabled(false);
        mnNuevo.setEnabled(true);
        mnEditar.setEnabled(true);
        refrescarGrid();
        limpiarText();
    }//GEN-LAST:event_mnGrabarActionPerformed

    private void mnEditarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnEditarMouseClicked

        SeleccionarRegistro();

    }//GEN-LAST:event_mnEditarMouseClicked

    private void mnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnEditarActionPerformed
        SeleccionarRegistro();
}//GEN-LAST:event_mnEditarActionPerformed

    private void mnEliminarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnEliminarMouseClicked
        GestorCancha.delete((Integer.parseInt(txtCodigo.getText())));
        refrescarGrid();
        limpiarText();
}//GEN-LAST:event_mnEliminarMouseClicked

    private void mnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnEliminarActionPerformed
        GestorCancha.delete((Integer.parseInt(txtCodigo.getText())));
        refrescarGrid();
        limpiarText();
}//GEN-LAST:event_mnEliminarActionPerformed

    private void tblCanchasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblCanchasMouseClicked
        SeleccionarRegistro();
    }//GEN-LAST:event_tblCanchasMouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JMenu mnEditar;
    private javax.swing.JMenu mnEliminar;
    private javax.swing.JMenu mnGrabar;
    private javax.swing.JMenu mnNuevo;
    private javax.swing.JTable tblCanchas;
    private javax.swing.JTextField txtCapacidad;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtDescripcion;
    private javax.swing.JTextField txtFechaUltMnat;
    // End of variables declaration//GEN-END:variables
    MiTableModel model = new MiTableModel();

    public void refrescarGrid(){
        model.lista = GestorCancha.queryAll();
        tblCanchas.setModel(model);
        model.fireTableChanged(null);
    }
    public void limpiarText(){
        txtCodigo.setText("");
        txtDescripcion.setText("");
        txtFechaUltMnat.setText("");
        txtCapacidad.setText("");

    }

    private void SeleccionarRegistro(){
        int filSel = tblCanchas.getSelectedRow();
        if (filSel != -1){
                intOpcion = MODIFICAR;
                mnEditar.setEnabled(false);
                mnNuevo.setEnabled(false);
                mnGrabar.setEnabled(true);
                mnEliminar.setEnabled(true);
                limpiarText();
                Cancha c = (Cancha) model.lista.get(filSel);
                txtCodigo.setText("" + c.getCodigo());
                txtCapacidad.setText("" + c.getCapacidad());
                txtDescripcion.setText(c.getDescripcion());
                txtFechaUltMnat.setText("" + c.getFechaUltMant());
        }

}

    class MiTableModel extends AbstractTableModel{
        ArrayList lista = new ArrayList();
        String [] titulos = {"Codigo", "Descripcion", "Cap.","Fec.Mant."};

        public int getRowCount() {
            return lista.size();
        }

        public int getColumnCount() {
            return titulos.length;
        }
        public String getColumnName(int column){
            String strTitle = new String();
            if (column < titulos.length){
            strTitle= titulos[column];
            }
            else {
                strTitle ="";
             }
            return strTitle;
        }

        public Object getValueAt(int fil, int col) {
            String valor = "";
                switch(col){
                case 0: valor = "" + ((Cancha)lista.get(fil)).getCodigo(); break;
                case 1: valor = "" + ((Cancha)lista.get(fil)).getCapacidad(); break;
                case 2: valor = "" + ((Cancha)lista.get(fil)).getDescripcion(); break;
                case 3: valor = "" + ((Cancha)lista.get(fil)).getFechaUltMant(); break;


            }
            return valor;
        }

    }


}
